<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23/023
 * Time: 14:02
 */

namespace app\Lib\Model\User;


use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\Model\DbModel\UserBase;

class GetUser
{
    private $keywordType;
    private $keyword;
    private $page;
    private $limit;
    private $isCount;
    private $Db;
    private $map =[];
    private $count = 0;
    public function __construct()
    {
        $this->Db = new UserBase();
    }

    /**
     * @param mixed $keywordType
     */
    public function setKeywordType($keywordType)
    {
        $this->keywordType = $keywordType;
    }

    /**
     * @param mixed $keyword
     */
    public function setKeyword($keyword)
    {
        $this->keyword = $keyword;
    }

    /**
     * @param mixed $page
     */
    public function setPage($page)
    {
        $this->page = $page;
    }

    /**
     * @param mixed $limit
     */
    public function setLimit($limit)
    {
        $this->limit = $limit;
    }

    /**
     * @param mixed $isCount
     */
    public function setIsCount($isCount)
    {
        $this->isCount = $isCount;
    }

    public function run(){
        $this->getMap();
        $this->getCount();
        if($this->isCount){
            return $this->count;
        }
        $data = $this->getList();
        return ['list'=>$data];
    }

    /**
     * 查询条件组装
     * @return array
     */
    private function getMap(){
        if(empty($this->keyword) || empty($this->keywordType)){
            return [];
        }
        $map = [];
        switch($this->keywordType){
            case 'account':
                $map  = [$this->Db->_account=>$this->keyword];
                break;
            case 'nickName':
                $map = [$this->Db->_nickname=>['like','%'.$this->keyword.'%']];
                break;
            case 'phone':
                $map = [$this->Db->_mobilephone=>['like','%'.$this->keyword.'%']];
                break;
        }
        $this->map = $map;
//        return $map;
    }

    /**
     * 获取总数量
     */
    private function getCount(){
        $table = $this->Db;
        $count = $table->where($this->map)->count('*');
        $this->count =  $count;
    }

    /**
     * 获取查询结果
     * @return array|mixed
     */
    private function getList(){
        if($this->count == 0){
            return [];
        }
        $table = $this->Db;
        $limit = $table->where($this->map)
            ->limit(($this->page-1)*$this->limit,$this->limit)->order('id desc')
            ->column($table->_id);
        return $this->handelArray($limit);
    }

    /**
     * 数据处理
     * @param $result
     * @return mixed
     */
    private function handelArray($result ){
        $table = $this->Db;
        foreach ($result as $v){
            $userModel = (new User($v));
            $role = $userModel->getRoleClass();
            $roleName = '未绑定角色';
            if($role instanceof RoleHandle){
                $roleName = $role->getName();
            }
            $lastTime = $userModel->getLastLoginTime();
            if($lastTime>0){
                $lastTime = date('Y-m-d H:i:s',$lastTime);
            }
            $data[$userModel->getUid()] = [
                $table->_id=>$userModel->getUid(),
                $table->_account=>$userModel->getAccount(),
                $table->_nickname=>$userModel->getNickName(),
                'roleName'=>$roleName,
                $table->_weixin=>$userModel->getWeiXin(),
                $table->_mobilephone=>$userModel->getPhone(),
                $table->_last_login_time=>$lastTime,
                $table->_sex=>$userModel->getSexStr(),
                $table->_account_status=>$userModel->isAllowLogin()?'正常':'禁用'
            ];
        }
        return $data;
    }
}